#include "Library_service_achieve.h"
#include "Library_data_achieve.h"
#include "Library_data.h"
#include "Tool.h"
#include <stdlib.h>
#include <time.h>
using namespace std;
using namespace LB;

//构造函数中动态创建Library_data_achieve对象
//lib_file调用loadbook函数将文件中的书籍数据加载到v_classbooks容器中
//lib_file调用loadusers函数将文件中的用户数据加载到v_users容器中
Library_service_achieve::Library_service_achieve()
{
	init_id(CSID_PATH,101);
	init_id(BKID_PATH,10001);
	init_id(USID_PATH,1001);
	lib_file = new Library_data_achieve;
	lib_file -> loadbook(v_classbooks);
	lib_file -> loadusers(v_users);
	//文件读写
}

//将参数传入的书种类类压入容器中
bool Library_service_achieve::addclass(Classbook &classbook)
{
	for( unsigned int i = 0; i < v_classbooks.size(); i++)
	{
		if(strcmp(classbook.m_classname,v_classbooks[i].m_classname) == 0)
		{
			//cout << "该书籍类已经存在　！"　<< endl;
			return false;
		}
	}
	//cout << "书籍类添加成功" << endl;
	v_classbooks.push_back(classbook);
	return true;
}

//根据id删除容器中的书类类
bool Library_service_achieve::deleteclass(int classbook_id)
{
	for( unsigned int i = 0; i < v_classbooks.size(); i++)
	{
		if(classbook_id == v_classbooks[i].m_cid)
		{
			v_classbooks.erase(v_classbooks.begin()+i);

			return true;
		}
	}
	return false;
}

//显示容器中所有书的种类名
void Library_service_achieve::listclass(void)
{
	loading();
	for( unsigned int i = 0; i < v_classbooks.size(); i++ )
	{
		cout << "书类ID: "<< v_classbooks[i].m_cid <<"  "<< v_classbooks[i].m_classname << endl;
	}	
}

//根据传入的种类id，找到对应的classbook对象，调用其addbook(Book &book)函数
bool Library_service_achieve::addbook(int classbook_id,Book &book)
{
	for( unsigned int i = 0; i < v_classbooks.size(); i++)
	{
		if( classbook_id == v_classbooks[i].m_cid)
		{
			v_classbooks[i].v_book.push_back(book);
			return true;
		}
	}
	return false;
}

//根据传入的种类id，找到对应的classbook对象，调用deletebook(int book_id)函数
bool Library_service_achieve::deletebook(int classbook_id,int book_id)
{
	for( unsigned int i = 0; i < v_classbooks.size(); i++ )
	{
		if( classbook_id == v_classbooks[i].m_cid)
		{
			for( unsigned int j = 0; j < v_classbooks[i].v_book.size(); j++)
			{
				if( v_classbooks[i].v_book[j].m_bid == book_id)
				{
					 v_classbooks[i].v_book.erase(v_classbooks[i].v_book.begin()+j);
					 return true;
				}
			}
			return false;
		}
	}
	return false;
}

//通过bookid循环使用classbook对象的find函数返回容器中对应的book对象，
//如果book在馆,通过userid找到对应user对象，
//并调用其borrowbook(int bookid,char* bookname)函数在maps添加新元素，若成功将vector<Classbook>中的书籍状态设置为不在馆,
//借书人id赋值为userid,借书次数borrow_times+1,如果不在馆，则只将book的预约人id设置为userid。
bool Library_service_achieve::borrowbook(int bookid,int userid)
{
	for( unsigned int i = 0; i < v_classbooks.size(); i++ )
	{
		for( unsigned int j = 0; j < v_classbooks[i].v_book.size(); j++)
		{
			if(v_classbooks[i].v_book[j].m_bid == bookid )
			{
				
				if( v_classbooks[i].v_book[j].at_library )
				{
					for( unsigned int k = 0; k < v_users.size();k++)
					{
						if(v_users[k].m_uid == userid)
						{
							if( v_users[k].borrowbook(bookid,v_classbooks[i].v_book[j].m_bookname) )
							{
								v_classbooks[i].v_book[j].at_library = false;
								v_classbooks[i].v_book[j].borrowid = userid;
								v_classbooks[i].v_book[j].borrow_times++;
								int days=get_days();
								v_classbooks[i].v_book[j].restday=days;
								cout<<"Rest hour:"<<(days / 3600)<<endl;
								

								return true;
							}
							else
							{
								cout << "借书已经满５本　不能继续借书　！";
							}
						}
					}
					cout << "用户ID错误　!" << endl;
					return false;

				}
				else
				{
					v_classbooks[i].v_book[j].orderid = userid;
					cout << "此书已借光　您已经预约成功　！"<< endl;
					return false;
				}
				
			}
		}	
	}
	cout << "书籍ID错误　！" << endl;
	return false;
}

//通过userid，find(==运算符)找到该user对象，调用user对象的revertbook函数将maps中

//对应的元素删除，并通过bookid循环调用classbook的find函数找到对应书籍book修改在馆状态，
//如果预约id不为0，则调用此id对应的user对象，调用其borrowbook(int bookid,char* bookname);
//并判断是否成功，若成功则将借书id设为预约id，预约id清0。
void Library_service_achieve::revertbook(int bookid,int userid)
{
	for( unsigned int i = 0; i < v_users.size(); i++)
	{
		if(userid == v_users[i].m_uid)
		{
			
			for( unsigned int j = 0; j < v_classbooks.size(); j++)
			{
				for( unsigned int k = 0; k < v_classbooks[j].v_book.size(); k++)
				{
					if( bookid == v_classbooks[j].v_book[k].m_bid )
					{
						int days=get_days();
						int d=days-v_classbooks[j].v_book[k].restday;
					
						if( d>30)
						{
							int superd=d-30;
							if((superd*0.2)<v_users[i].balance)
							{
								v_users[i].balance-=(d*0.2);
								v_classbooks[j].v_book[k].restday=0;
								
								if(!v_users[i].revertbook(bookid))
								{
									cout<<"还书失败！"<<endl;
									return;
								}
								cout << "您已成功还书　！"<< endl;
								
								cout<<"余额:"<<v_users[i].balance<<endl;
								if( v_classbooks[j].v_book[k].orderid != 0)
								{
									for( unsigned int n = 0; n < v_users.size(); n++)
									{
										if( v_users[n].m_uid == v_classbooks[j].v_book[k].orderid)
										{
											if( v_users[n].borrowbook(bookid,v_classbooks[j].v_book[k].m_bookname) )
											{
												v_classbooks[j].v_book[k].borrowid = v_classbooks[j].v_book[k].orderid;
												v_classbooks[j].v_book[k].orderid = 0;
												cout << "预约者已成功借阅您还的这本书　！" << endl;
											
											}
										}
									}
								}
								else
								{
										v_classbooks[j].v_book[k].at_library = true;
								}
							}
							else
							{
								cout<<"余额:"<<v_users[i].balance<<endl;
								cout<<"您的余额不足，请充值！"<<endl;
								return;
							}
						}	
					}				
				}
			}
			
		}
		
	}
}

//显示当前书类下所有的书籍，找到对应的classbook对象，调用其listbook(void)函数
void Library_service_achieve::listbook(int classbook_id)
{
	loading();
	for( unsigned int i = 0; i < v_classbooks.size(); i++ )
	{
		if( v_classbooks[i].m_cid == classbook_id)
		{
			for( unsigned int j = 0; j < v_classbooks[i].v_book.size(); j++)
			{
				cout << j+1 << " :" << v_classbooks[i].v_book[j].m_bookname << endl;
			}
			
		}		
	}
	
}

//显示所有书籍，循环调用classbook对象的listbook()函数
void Library_service_achieve::listallbook(void)
{
	/*char *p[10] = 
	{"非常的好看","很经典的","引人深思","行云流水","笔走龙蛇","妙笔生花","字字珠玑","书中自有颜如玉","精妙绝伦","爱不释手"};*/

	loading();
	srand(time(NULL));
	for( unsigned int i = 0; i < v_classbooks.size(); i++)
	{
		cout << i+1<< "、"<< v_classbooks[i].m_cid <<"  "<<v_classbooks[i].m_classname <<endl;		
		for( unsigned int j = 0; j < v_classbooks[i].v_book.size(); j++)
		{	
			/*int id = rand()%3 +1001;
			int k = rand()%10;
			v_classbooks[i].v_book[j].insertcomment(id,p[k]);*/
			//v_classbooks[i].v_book[j].v_comment.clear();
			//v_classbooks[i].v_book[j].praise_val = rand()%5+1;
			//v_classbooks[i].v_book[j].borrow_times = rand()%10 +2;
			v_classbooks[i].v_book[j].showbook();
			//cout << "	"<< v_classbooks[i].v_book[j].m_bid << "、" <<"<<" <<v_classbooks[i].v_book[j].m_bookname <<">>"<< endl;
		}
		cout << endl;
	}

}

//将参数传入的user对象压入v_users容器中
void Library_service_achieve::adduser(User &user)
{
	v_users.push_back(user);
	cout << "增加用户成功！"<< endl;
}
bool Library_service_achieve::addbalance(int id,float balance)
{
	for(unsigned int i=0;i<v_users.size();i++)
	{
		if(v_users[i].m_uid==id)
		{
			v_users[i].balance+=balance;
			return true;
		}
	}
	return false;
}
//根据id找到容器中对应的user，并删除
void Library_service_achieve::deleteuser(int user_id)
{
	for( unsigned int i = 0; i < v_users.size(); i++)
	{
		if(user_id == v_users[i].m_uid)
		{
			v_users.erase(v_users.begin()+i);
			cout << "删除用户成功！" << endl;
		}
	}
	cout << "删除用户失败　ID错误 ！" << endl;
}

//显示所有用户
void Library_service_achieve::listuser(void)
{
	cout << "*************************************************" << endl;
	for( unsigned int i = 0; i < v_users.size(); i++)
	{
		cout <<i+1 << "、" <<v_users[i].m_uid<< "  "<< v_users[i].m_username << endl;
	}
}

/*lib_file调用savebook函数将v_classbooks容器中书籍数据存储到文件中
lib_file调用saveusers函数将v_users容器中的用户数据存储到文件中
释放lib_file*/
Library_service_achieve::~Library_service_achieve()
{
	lib_file -> savebook(v_classbooks);
	lib_file -> saveusers(v_users);
}
